package edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import edu.domain.Goods;
import edu.domain.GoodsCollect;
import edu.service.GoodsCollectService;
import edu.service.GoodsService;
import edu.support.base.BasePage;
import edu.support.base.controller.AdminBaseController;
import edu.support.mybatis.MybatisWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * (Goods)表控制层
 *
 * @date 2022-03-09 23:09:09
 */

@Controller
@RequestMapping("goods")
public class GoodsController extends AdminBaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCollectService goodsCollectService;

    /**
     * 列表
     *
     * @param model
     * @param goods
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Goods goods, BasePage rowPage) {
        QueryWrapper<Goods> wrapper = new MybatisWrapper<Goods>()
                .eq("status","上架")
                .like("category", goods.getCategory())
                .like("name", goods.getName())
                .orderByDesc("create_time");
        PageInfo<Goods> pageInfo = goodsService.page(rowPage, wrapper);
        setModelAttribute(model, pageInfo);
        return "goods/list";
    }


    /**
     * 详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        Goods goods = goodsService.getById(id);
        // 是否收藏了
        int collectCount = 0;
        if (null != loginUser()) {
            collectCount = goodsCollectService.count(new QueryWrapper<GoodsCollect>().eq("user_id", loginUserId()).eq("goods_id", id));
        }


        model.addAttribute("goods", goods);
        model.addAttribute("collectCount", collectCount);
        return "goods/detail";
    }


    /**
     * 收藏
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("collect/{id}")
    public String collect(@PathVariable Long id, RedirectAttributes model) {
        GoodsCollect goodsCollect = goodsCollectService.getOne(new QueryWrapper<GoodsCollect>().eq("user_id", loginUserId()).eq("goods_id", id));
        if (goodsCollect == null) {
            goodsCollect = new GoodsCollect();
            goodsCollect.setGoodsId(id);
            goodsCollect.setUserId(loginUserId());
            goodsCollectService.save(goodsCollect);
            return refresh("收藏成功", model);
        } else {
            goodsCollectService.removeById(goodsCollect);
            return refresh("取消收藏成功", model);
        }
    }


    /**
     * 更新
     *
     * @param goods
     * @return
     */
    @RequestMapping("update")
    public String update(Goods goods, RedirectAttributes attributes) {
        goodsService.saveOrUpdate(goods);
        return redirect("/goods/list", "操作成功", attributes);
    }

}


